package top.zsyle.pet.service.impl;

import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import top.zsyle.basic.config.BussinessException;
import top.zsyle.basic.jwt.UserInfo;
import top.zsyle.basic.util.BaiduAuditUtils;
import top.zsyle.basic.util.ThreadLocalUtil;
import top.zsyle.org.domain.Employee;
import top.zsyle.org.mapper.EmployeeMapper;
import top.zsyle.pet.domain.Pet;
import top.zsyle.pet.domain.PetDetail;
import top.zsyle.pet.domain.PetOnlineAuditLog;
import top.zsyle.pet.mapper.PetDetailMapper;
import top.zsyle.pet.mapper.PetMapper;
import top.zsyle.pet.mapper.PetOnlineAuditLogMapper;
import top.zsyle.pet.service.IPetService;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginContext;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-12-05
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void add(Pet pet) {
        // ************** 添加宠物 **************
        // 状态 默认下架
        pet.setState(1);
        // 创建时间
        pet.setCreatetime(new Date());
        // 店铺id
//        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
//        if (!Objects.isNull(userInfo.getShop())) {
//            pet.setShopId(userInfo.getShop().getId());
//        }
        //todo 默认【屌爆了公司】
        pet.setShopId(27L);
        super.add(pet);
//        // *************** 添加详情 *******************
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //1.校验参数
        if (Collections.isEmpty(ids)){
            throw new BussinessException("上架的宠物不能为空哦！！");
        }
        //2.过滤掉已经上架的id
        List<Pet> pets = petMapper.findByIdsAndState(ids);
        if (Collections.isEmpty(pets)){
            return;
        }
        //3.百度ai审核
        List<PetOnlineAuditLog> auditLogs = new ArrayList<>();
        List<Long> petIds = new ArrayList<>();
        pets.forEach(pet->{
            StringBuilder sb = new StringBuilder();
            //判断宠物名字是否合规
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append("宠物名称审核不通过，名字："+pet.getName());
            }
            //审核图片是否合规
            //我们的resources是使用, 隔开的 我们需要一个一个去审核
            String resources = pet.getResources();
            Arrays.stream(resources.split(",")).forEach(img->{
                if (!BaiduAuditUtils.ImgCensor(img)){
                    sb.append("宠物图片审核不通过，名字为："+pet.getName());
                }
            });
            //添加的日志
            if (sb.length()>0){
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setPetId(pet.getId());
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setState(0);
                //获取当前审核人
                UserInfo userInfo = ThreadLocalUtil.getUserInfo();
                String username = userInfo.getLogininfo().getUsername();
                Employee emp = employeeMapper.findByUsername(username);
                auditLog.setAuditId(emp.getId());
                auditLogs.add(auditLog);
                return;
            }
            // 修改状态
            petIds.add(pet.getId());
        });
        //通过 修改状态为上架 --- 上架时间 下架时间
        if(!Collections.isEmpty(petIds)){
            petMapper.batchUpdate(petIds);
        }
        if (!Collections.isEmpty(auditLogs)){
            //未通过 不修改状态添加日志
            petOnlineAuditLogMapper.batchAdd(auditLogs);
        }
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //1.校验参数
        if (Collections.isEmpty(ids)){
            throw new BussinessException("下架的宠物不能为空哦！！");
        }
        //2.过滤掉已经下架的id
        List<Pet> pets = petMapper.findByIdsAndStateOff(ids);
        if (Collections.isEmpty(pets)){
            return;
        }
        List<Long> petIds = pets.stream().map(Pet::getId).collect(Collectors.toList());
        if (!Collections.isEmpty(petIds)){
            //修改state=0
            petMapper.batchUpdateOff(petIds);
        }
    }
}
